package com.oyjd.ybx.business.favorite.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.ybx.bean.po.GoodsInfo;
import com.oyjd.ybx.bean.po.ToFavorite;
import com.oyjd.ybx.bean.po.ToLike;
import com.oyjd.ybx.business.favorite.ToFavoriteExVO;
import com.oyjd.ybx.business.favorite.dao.FavoriteDao;
import com.oyjd.ybx.business.favorite.service.FavoriteService;

/**
 * 
 * 收藏用户业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class FavoriteServiceImpl implements FavoriteService {
	private FavoriteDao dao;

	@Autowired
	public void setDao(FavoriteDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取收藏用户列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @param account
	 *            帐号
	 * @return
	 */
	public List<ToFavoriteExVO> getList(String account) {
		return dao.getList(account);
	}

	/**
	 * 增加收藏用户
	 * 
	 * @param goodsId
	 *            商品ID
	 * @param account
	 *            用户帐号
	 * @param custIp
	 *            客户端IP
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	public boolean add(String goodsId, String account, String custIp) {
		boolean flag = false;
		List<ToFavorite> list = (List<ToFavorite>) dao.queryByHql("from " + ToFavorite.class.getName() + " where goodsId=? and account=?", new Object[] { goodsId, account });
		if (0 == list.size()) {
			ToFavorite toFavorite = new ToFavorite();
			toFavorite.setGoodsId(goodsId);
			toFavorite.setAccount(account);
			toFavorite.setCustIp(custIp);
			toFavorite.setCreateTime(new Date());
			dao.create(toFavorite);

			dao.executeHql("update " + GoodsInfo.class.getName() + " g set g.countFavorite=(select count(t.favoriteId) from " + ToFavorite.class.getName() + " t where t.goodsId=g.goodsId) where g.goodsId=?", new Object[] { goodsId });
			flag = true;
		}
		return flag;
	}

	/**
	 * 增加收藏用户
	 * 
	 * @param goodsId
	 *            商品ID
	 * @param account
	 *            用户帐号
	 * @param custIp
	 *            客户端IP
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	public boolean addLike(String goodsId, String account, String custIp) {
		boolean flag = false;
		List<ToLike> list = (List<ToLike>) dao.queryByHql("from " + ToLike.class.getName() + " where goodsId=? and account=?", new Object[] { goodsId, account });
		if (0 == list.size()) {
			ToLike toLike = new ToLike();
			toLike.setGoodsId(goodsId);
			toLike.setAccount(account);
			toLike.setCustIp(custIp);
			toLike.setCreateTime(new Date());
			dao.create(toLike);

			dao.executeHql("update " + GoodsInfo.class.getName() + " g set g.countLike=(select count(t.likeId) from " + ToLike.class.getName() + " t where t.goodsId=g.goodsId) where g.goodsId=?", new Object[] { goodsId });
			flag = true;
		}
		return flag;
	}

	/**
	 * 删除收藏用户
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String id) {
		dao.deleteById(ToFavorite.class, id);
		dao.executeHql("update " + GoodsInfo.class.getName() + " g set g.countFavorite=(select count(t.favoriteId) from " + ToFavorite.class.getName() + " t where t.goodsId=g.goodsId)");
	}

}
